package org.goldfish.demo.apicall.analysis.code;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import lombok.Getter;
import org.goldfish.demo.apicall.analysis.code.di.SpringFXMLLoader;
import org.goldfish.demo.apicall.analysis.code.select.PDFSelectorController;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import java.io.IOException;

@ComponentScan("org.goldfish.demo.apicall.analysis.code")

public class CodeAnalyzeApplication extends Application {

    @Getter
    private static ConfigurableApplicationContext springContext;

    @Override
    public void start(Stage stage) throws IOException {
        init();
        FXMLLoader fxmlLoader = new SpringFXMLLoader("hello-view.fxml");
        javafx.scene.Parent parent = fxmlLoader.load();
        Scene scene = new Scene(parent);
        stage.setTitle("代码分析工具");
        stage.setScene(scene);
        stage.show();
    }

    @Override
    public void init() {
        springContext = (springContext != null) ? springContext
                : new AnnotationConfigApplicationContext(CodeAnalyzeApplication.class);
    }

    @Override
    public void stop() {
        // 清理PDF处理线程等资源
        try {
            if (springContext != null && springContext.isActive()) {
                PDFSelectorController pdfController = springContext.getBean(PDFSelectorController.class);
                if (pdfController != null) {
                    pdfController.cleanup();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 关闭Spring上下文
        if (springContext != null && springContext.isActive()) {
            try {
                springContext.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}